package com.jintian.smart.kernel.flowable.component.action.task;

import java.util.List;
import java.util.concurrent.Callable;

import com.jintian.smart.kernel.core.flow.FlowAction;
import com.jintian.smart.kernel.core.flow.ProcessControl;
import com.jintian.smart.kernel.flowable.component.ProcessContextProvider;
import com.jintian.smart.kernel.flowable.component.action.base.AbstractTaskAction;
import com.jintian.smart.kernel.flowable.context.DefaultProcessContext;
import com.jintian.smart.kernel.flowable.flowable.cmd.AddHistoricVariableCmd;
import com.jintian.smart.kernel.flowable.flowable.cmd.TaskCompleteCmd;
import com.jintian.smart.kernel.flowable.service.CommentService;
import org.flowable.bpmn.model.FlowNode;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskInfo;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 完成当前任务，包含委派任务处理
 */
@Component
public class CompleteTaskAction extends AbstractTaskAction {
    @Override
    public FlowAction getAction() {
        return FlowAction.complete;
    }

    @Autowired
    private CommentService commentService;

    @Override
    protected List<TaskInfo> innerExecute(TaskEntity task, ProcessControl processControl) throws Exception {
        ProcessInstance pi = this.getProcessInstance(task.getProcessInstanceId());
        try (DefaultProcessContext context = (DefaultProcessContext) this.processContextProvider.createInstance(
                pi.getProcessDefinitionKey(), task.getTaskDefinitionKey(), this.getAction(),
                task.getProcessInstanceId(), task.getId(), pi.getBusinessKey(), processControl);) {
            this.beforeAction(task, context, this.getAction());
            CompleteTaskAction self = this;
            List<TaskInfo> ret = this.invokeAction(context, new Callable<List<TaskInfo>>() {
                @Override
                public List<TaskInfo> call() throws Exception {
                    return self.completeTask(task, context);
                }
            });
            return ret;
        }
    }

    private List<TaskInfo> completeTask(TaskEntity currentTask, DefaultProcessContext context) {
        // 设置
        this.managementService().executeCommand(new TaskCompleteCmd(currentTask, context.getOperator().getId(), context.getVariables(),
                context.getVariablesLocal(), context.getTransientVariables(), context.getTransientVariablesLocal()));
        List<TaskInfo> newTasks = ProcessContextProvider.getCurrentProcessContext().getNewTasks();
        if (newTasks.size() == 0) {
            // 办结
            this.managementService().executeCommand(new AddHistoricVariableCmd(currentTask.getId(), AddHistoricVariableCmd.HistoricVariable_TaskInstance, context.getVariables()));
        } else {
            // 设置默认用户
            for (TaskInfo t : newTasks) {
                checkNewTaskExecutor((TaskEntity) t);
            }
        }
        commentService.finish(context);
        return newTasks;
    }

    @Override
    public List<FlowNode> queryTaskNextActivityNode(Task task) {
        return this.getNextFlowNode(task);
    }
}
